<template>
	<view class="sl-contant">
		<!-- <image
			style="width: 129px; position: absolute; top: 2%; right: 12%"
			src="/static/img/23.png"
			mode="widthFix"></image> -->
		<view>
			<image class="avatar" style="width: 129px" :src="imageurl + imgs" mode="widthFix"></image>
		</view>
		<view class="tit">手机号快捷登录</view>
		<view class="main">
			<u--input
				:adjustPosition="false"
				fontSize="16"
				v-model="phone"
				maxlength="11"
				type="number"
				placeholder="手机号"
				border="none"
				clearable></u--input>
			<!-- 注意：由于兼容性差异，如果需要使用前后插槽，nvue下需使用u--input，非nvue下需使用u-input -->
			<!-- #ifndef APP-NVUE -->
			<u-input border="none" placeholder="验证码" type='number' v-model="verifycode">
			<!-- #endif -->
			<!-- #ifdef APP-NVUE -->
			<u--input placeholder="验证码" type='number' v-model="verifycode">
			<!-- #endif -->
					<template slot="suffix">
						<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
						<u-button @tap="getCode" :text="tips" type="success" ></u-button>
					</template>
			<!-- #ifndef APP-NVUE -->
			</u-input>
			<!-- #endif -->
			<!-- #ifdef APP-NVUE -->
			</u--input>
			<!-- #endif -->
			<view class="flex" style="margin-top: 10px; justify-content: space-between">
				<view class="left flex">
					<view style="font-size: 12px; color: #8e9090">还没有账号？</view>
					<view @click="register" style="font-size: 12px; color: #2447FF">立即注册</view>
				</view>
				<view class="right">
					<view @click="loginback" style="font-size: 12px; color: #2447FF">返回账号登陆</view>
				</view>
			</view>
			<view @click="login" class="flex login-style">登录</view>
			<view v-if="tabbar" class="flex" style="padding-top: 40px">
				<u-checkbox-group shape="circle">
					<u-checkbox :checked="true" @change="change" activeColor="#2447FF" v-model="checked"></u-checkbox>
				</u-checkbox-group>
				<view style="font-size: 14px; margin-left: 10px">
					登录并代表您已同意
					<text @click="nav(1)" style="color: #2447FF">《用户协议》</text>
					＆
					<text style="color: #2447FF" @click="nav(2)">《隐私协议》</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import config from '../../common/config/index.js';
	export default {
		data() {
			return {
				imageurl: config.imgurl,
				imgs: '',
				phone: '', // 手机号
				verifycode: '', // 密码
				tabbar: true,
				checked: true,
				tips: '',
				value: ''
			};
		},
		onShow() {
			// #ifdef H5
			let status = navigator.userAgent.toLowerCase();
			if (status.match(/MicroMessenger/i) == 'micromessenger') {
				this.auth();
			} else {
			}
			// #endif
		},
		onLoad() {
			this.arr_img();
		},
		methods: {
			auth() {
				let d_url = window.location.href;
				let red_url = encodeURIComponent(d_url);
				this.wx_code = this.getUrlCode().code;
				let appid = 'wxf1322c2aa551e08c';
				if (this.wx_code == null || this.wx_code === '' || this.wx_code == undefined) {
					var wxUrl =
						'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
						appid +
						'&redirect_uri=' +
						red_url +
						'&response_type=code' +
						'&scope=snsapi_base' +
						'&state=STATE#wechat_redirect';
					window.location.href = wxUrl;
				} else {
					// let that = this;
					// uni.request({
					// 	url: that.purl + 'wechat.wechat/jingmo',
					// 	method: "POST",
					// 	header: {
					// 		token: ''
					// 	},
					// 	data: {
					// 		code: that.wx_code
					// 	},
					// 	success(res) {
					// 		console.log(res.data);
					// 		that.info = res.data.data
					// 		console.log(that.info);
					// 	},
					// 	complete(res) {
					// 		if (res.statusCode == 500) {
					// 			var url = window.location.href; //获取当前页面的url
					// 			if (url.indexOf("?") != -1) { //判断是否存在参数
					// 				url = url.replace(/(\?|#)[^'"]*/, ''); //去除参数
					// 				window.history.pushState({}, 0, url + '#/pages/me/reg_shanghu/reg_shanghu');
					// 				that.auth()
					// 			}
					// 		}
					// 	},
					// })
				}
			},
			getUrlCode() {
				// 截取url中的code方法
				var url = window.location.href; //获取打开的公众号的路径
				let winUrl = url;
				var theRequest = new Object();
				if (url.indexOf('?') != -1) {
					var str = url.substr(url.indexOf('?') + 1);
					var strs = str.split('&');
					for (var i = 0; i < strs.length; i++) {
						var items = strs[i].split('=');
						theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
					}
				} else {
				}
				return theRequest;
			},
			async arr_img() {
				let res = await this.$http.index.login_img({});
				this.imgs = res.data.logo;
			},
			register() {
				uni.navigateTo({
					url: 'register',
				});
			},
			loginback() {
				uni.navigateTo({
					url: '/pages/login/login',
				});
			},
			change(e) {
				this.checked = e;
			},
			async login() {
				if (!this.checked) {
					this.$tips('请先同意协议');
					return;
				}
				if (this.phone == '' || this.phone == null) {
					uni.showToast({
						icon: 'none',
						title: '请先填写账号',
					});
					return false;
				}
				if (this.verifycode == '') {
					uni.showToast({
						icon: 'none',
						title: '请先填写验证码',
					});
					return false;
				}

				let res = await this.$http.index.mobilelogin({
					mobile: this.phone,
					mobilecode: this.verifycode,
					openid: this.wx_code,
				})
				if (res.code == 1) {
					this.$tips('登录成功');
					let token = res.data.userinfo.token;
					uni.setStorageSync('token', token);
					// uni.setStorageSync('mobile',this.phone)
					console.log(res.data)
					if(res.data['goSetPwd ']==1){
					// if(res.data['goSetPwd ']==0){
						setTimeout(() => {
							uni.reLaunch({
								url: `/pages/login/smsloginpwd?m=${this.phone}`,
							});
						}, 1000);
					}
					else{
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index',
							});
						}, 1000);
					}
				} else {
					this.$tips(res.msg);
				}
			},
			nav(i) {
				switch (i) {
					case 1:
						uni.navigateTo({
							url: '/pages/me/agreement',
						});
						break;
					case 2:
						uni.navigateTo({
							url: '/pages/me/privacy',
						});
						break;
				}
			},			
			codeChange(text) {
				this.tips = text;
			},
			async getCode() {
				if (this.phone == '') {
					this.$tips('请先输入手机号')
					return
				}
				if (this.$refs.uCode.canGetCode) {
					let res = await this.$http.index.getcode({
						phone: this.phone,
						type: 'mobilelogin',
					})
					if (res.code == 1) {
						uni.$u.toast('验证码已发送');
						this.$refs.uCode.start();
					} else {
						uni.$u.toast(res.msg);
					}
					// // 模拟向后端请求验证码
					// uni.showLoading({
					// 	title: '正在获取验证码'
					// })
					// setTimeout(() => {
					// 	uni.hideLoading();
					// 	// 这里此提示会被this.start()方法中的提示覆盖
					// 	uni.$u.toast('验证码已发送');
					// 	// 通知验证码组件内部开始倒计时
					// 	this.$refs.uCode.start();
					// }, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
		},
	};
</script>

<style lang="scss">
	.flex {
		display: flex;
		align-items: center;
	}
	
	/deep/ .u-button--success {
		background-color: transparent !important;
		border: none !important;
		color: #2447FF !important;
		font-size: 16px !important;
	}
	
	.u-button--normal{
		padding: 0;
	}
	
	.sl-contant {
		background-image: url('@/static/font/57.png');
		background-repeat: no-repeat;
		background-size: cover;
		padding: 104px 30px 0px;

		.avatar {
			width: 60px;
			border-radius: 15px;
		}

		.tit {
			font-size: 20px;
			font-weight: bold;
			color: #1d1f20;
			margin-top: 15px;
		}

		.u-input {
			padding: 15px !important;
			background: #fff;
			border-radius: 10px;
			margin-top: 15px;
		}

		.input-placeholder {
			font-size: 16px !important;
		}

		.login-style {
			margin-top: 30px;
			justify-content: center;
			background-color: #2447FF;
			// box-shadow: 0px 3px 6px 1px #ffbcbb;
			border-radius: 20px;
			height: 40px;
			font-size: 16px;
			color: #fff;
		}
	}
	/deep/.u-button{
		height: 100% !important;
	}
</style>
